package tq.com.tqteacher.mine.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONObject;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.net.TQNetInterface;

public class TQClassCardFragment extends BaseFragment {
    private CardInfo mCardInfo = new CardInfo();

    private TextView mTile;
    private TextView mPublish;

    private EditText mDesc;
    private EditText price;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View lView = inflater.inflate(R.layout.fragment_classcard_layout, container, false);

        mTile = lView.findViewById(R.id.title2);
        mPublish = lView.findViewById(R.id.desc);

        mDesc = lView.findViewById(R.id.content);
        mDesc.setEnabled(false);
        price = lView.findViewById(R.id.price);
        price.setEnabled(false);

        loadData();
        return lView;
    }

    private void loadData(){
        TQNetInterface.getClsCardInfo(String.valueOf(TQLogin.getCurrent().getUserInfo().getClzid()), new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    AlertDialogHelper.show(TQClassCardFragment.this.getContext(), pNetResult.getErrMsg());
                    return;
                }

                if(pNetResult.getData() == null) return;

                JSONObject lObject = pNetResult.getData().optJSONObject("card");
                mCardInfo.fromJson(lObject);

                bindData();
            }
        });
    }

    private void bindData(){
        mTile.setText(mCardInfo.name);
        mPublish.setText(String.format("预计发布：共%d次", mCardInfo.publish_cnt));

        mDesc.setText(mCardInfo.desc);
        price.setText(String.format("%.2f", mCardInfo.price));
    }

    private class CardInfo{
        private String name ;
        private String desc;
        private int subject;
        private int publish_cnt;
        private float price;
        private boolean free_ship;


        public void fromJson(JSONObject pJSONObject){
            if(pJSONObject == null)return;

            name = pJSONObject.optString("name");
            desc = pJSONObject.optString("desc");

            subject = pJSONObject.optInt("subject");
            price = (float) pJSONObject.optDouble("price");
            free_ship = pJSONObject.optBoolean("free_ship");

            publish_cnt = pJSONObject.optInt("publish_cnt");
        }
    }
}
